Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Continue

Continue statement

The continue statement in Python is used to skip the rest of the code inside the enclosing loop for the current iteration and move on to the next iteration of the loop. It’s often used in while and for loops. Here are some examples:

Skipping even numbers in a list:

Skipping numbers in a list using continue keyword numbers = [1, 2, 3, 4, 5, 6] for num in numbers: if num % 2 == 0: continue print(num)

Output

1 3 5
In this example, the continue statement skips the print statement for even numbers. So, only odd numbers are printed.

Skipping a specific character in a string:

Skipping a specific character in a string using continue keyword word = "Hello" for char in word: if char == 'e': continue print(char)

Output

H l l o
In this example, the continue statement skips the print statement when the character is ‘e’. So, all characters except ‘e’ are printed.

Skipping negative numbers in a list:

Skipping negative numbers in a list using continue keyword numbers = [1, -2, 3, -4, 5, -6] for num in numbers: if num < 0: continue print(num)

Output

1 3 5
In this example, the continue statement skips the print statement for negative numbers. So, only positive numbers are printed.

Skipping a loop iteration in a while loop:

Skipping a loop iteration in a while loop using continue keyword count = 0 while count < 5: count += 1 if count == 3: continue print(count)

Output

1 2 4 5
In this example, the continue statement skips the print statement when count is 3. So, all numbers except 3 are printed.

Skipping specific elements in a dictionary:

Skipping elements in a dictionary using continue keyword person = {"name": "Alice", "age": 25, "city": "New York"} for key in person: if key == "age": continue print(key, person[key])

Output

name Alice city New York
In this example, the continue statement skips the print statement when the key is ‘age’. So, all key-value pairs except ‘age’ are printed.

  📌TAGS

★python ★ loop ★ while-else

Tutorials